package com.bicon.botu.mall.component.api;

import com.bicon.botu.mall.component.entity.HistorySearch;
import com.bicon.botu.mall.component.service.IHistorySearchService;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;

import com.bicon.botu.core.base.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 搜索历史表 前端控制器
 *
 * @author 张凡
 * @date 2017/7/19 下午 14:49
 */
@RestController
@CrossOrigin
@RequestMapping("/V1.0.0/api/historySearch")
public class HistorySearchApi extends BaseController {
    @Autowired
    IHistorySearchService iHistorySearchService;

    /**
     * 保存用户搜索记录
     *
     * @param searchWord 搜索关键字
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult add(@RequestParam("type") String type,
                          @RequestParam("searchWord") String searchWord,
                          @RequestParam("total") Integer total) {
        if (StringUtils.isBlank(searchWord)) {
            return sendResult200();
        }
        return iHistorySearchService.add(getUserId(), type, searchWord, total);
    }

    /**
     * 批量逻辑删除用户搜索记录(单条)
     *
     * @param id 搜索词id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult delete(@RequestParam("id") String id) {
        return iHistorySearchService.deleteOneHistory(id);
    }

    /**
     * 逻辑清空用户搜索记录
     *
     * @return
     */
    @RequestMapping(value = "/deleteAll", method = RequestMethod.POST)
    public BaseResult deleteAll(@RequestParam("type") String type) {
        return iHistorySearchService.deleteAllHistory(type,getUserId());
    }
    /**
     * 初始化用户的最新20条记录历史搜索到redis
     *
     * @param type 模块类型
     * @return
     */
    @RequestMapping(value = "/initHistorySearchToRedis", method = RequestMethod.POST)
    public BaseResult initHistorySearchToRedis(@RequestParam("type") String type) {
        return iHistorySearchService.initHistorySearchToRedis(getUserId(),type);
    }

    /**
     * 获取用户的历史搜索详情
     * @param type 模块类型
     * @return
     */
    @RequestMapping(value = "/getHistorySearchDetailFromRedis", method = RequestMethod.POST)
    public BaseResult getHistorySearchDetailFromRedis(@RequestParam("type") String type) {
        List<HistorySearch> historySearchList = iHistorySearchService.getHistorySearchDetailFromRedis(getUserId(),type);
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, historySearchList);

    }
}
